package org.jwos.platform.desktop.web;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jwos.platform.ajax.JSONProxy;
import org.jwos.platform.ajax.JSONUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class JSONServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		try {
			String data = JSONUtil.getDataString(request.getInputStream(),request.getContentLength());
			ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
			
			JSONProxy proxy = new JSONProxy(context,this.getClass().getClassLoader());
			String result = proxy.call(data);
			
			response.reset();
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(result);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
}
